<p>To use WireMock to mock a specific API, you can follow these
steps:</p>
<ol type="1">
<li><p><strong>Download and Start WireMock</strong>:</p>
<ul>
<li><p>Download the WireMock JAR file from the official
website.</p></li>
<li><p>Start WireMock by running the JAR file in the command line:</p>
<div class="sourceCode" id="cb1"><pre
class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="ex">java</span> <span class="at">-jar</span> wiremock-standalone-<span class="op">&lt;</span>version<span class="op">&gt;</span>.jar <span class="at">--port</span> 8080</span></code></pre></div></li>
</ul></li>
<li><p><strong>Create a Stub Mapping</strong>:</p>
<ul>
<li><p>Create a JSON or YAML file to define the stub mapping. This file
will specify the request criteria and the response to return. Here is an
example JSON file named <code>stub_mapping.json</code>:</p>
<div class="sourceCode" id="cb2"><pre
class="sourceCode json"><code class="sourceCode json"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">{</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>  <span class="dt">&quot;request&quot;</span><span class="fu">:</span> <span class="fu">{</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>    <span class="dt">&quot;method&quot;</span><span class="fu">:</span> <span class="st">&quot;GET&quot;</span><span class="fu">,</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>    <span class="dt">&quot;urlPath&quot;</span><span class="fu">:</span> <span class="st">&quot;/api/resource&quot;</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a>  <span class="fu">},</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>  <span class="dt">&quot;response&quot;</span><span class="fu">:</span> <span class="fu">{</span></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>    <span class="dt">&quot;status&quot;</span><span class="fu">:</span> <span class="dv">200</span><span class="fu">,</span></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a>    <span class="dt">&quot;body&quot;</span><span class="fu">:</span> <span class="st">&quot;Mocked Response&quot;</span><span class="fu">,</span></span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a>    <span class="dt">&quot;headers&quot;</span><span class="fu">:</span> <span class="fu">{</span></span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a>      <span class="dt">&quot;Content-Type&quot;</span><span class="fu">:</span> <span class="st">&quot;application/json&quot;</span></span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a>    <span class="fu">}</span></span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a>  <span class="fu">}</span></span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a><span class="fu">}</span></span></code></pre></div></li>
</ul></li>
<li><p><strong>Import the Stub Mapping</strong>:</p>
<ul>
<li><p>Import the stub mapping into WireMock. You can do this via the
HTTP Admin API or by placing the file in the <code>mappings</code>
directory of the WireMock installation.</p></li>
<li><p>Using the HTTP Admin API (assuming WireMock is running on
localhost:8080):</p>
<div class="sourceCode" id="cb3"><pre
class="sourceCode bash"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="ex">curl</span> <span class="at">-X</span> POST <span class="at">-d</span> @stub_mapping.json http://localhost:8080/__admin/mappings/new</span></code></pre></div></li>
</ul></li>
<li><p><strong>Configure Your Client</strong>:</p>
<ul>
<li>In your application, configure the base URL or domain of the API to
point to the WireMock server, for example,
<code>http://localhost:8080</code>.</li>
</ul></li>
<li><p><strong>Test the Mocked API</strong>:</p>
<ul>
<li>Now, when your application sends a GET request to
<code>http://localhost:8080/api/resource</code>, WireMock will intercept
the request and return the predefined response.</li>
</ul></li>
</ol>
<p>This setup will intercept requests to <code>/api/resource</code> with
a GET method and respond with a 200 status code along with the body
“Mocked Response” and the specified content type.</p>
<p>Remember, this is a simplified example. WireMock supports many
advanced features, including request verification, response templating,
and more. For detailed documentation and advanced usage, refer to the
official <a href="http://wiremock.org/docs/">WireMock
documentation</a>.</p>
